package com.aierjun.ani.net.OkGo.okupload;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by Ani_aierJun on 2017/10/16.
 */

public class UpLoadDBHelper extends SQLiteOpenHelper {
    private static final String TABLE_NAME = "Ani_Upload";
    private static final String DB_NAME = "Ani.db";
    private static final String ID = "ID";
    private static final String IMGPATH = "ImgPath";
    private static final String TAG = "Tag";
    private static final String NAME = "Name";
    private static final String STATUS = "Status";
    private static final String IMG = "Img";

    public UpLoadDBHelper(Context context, int version) {
        super(context, DB_NAME, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table " + TABLE_NAME
                + "( " + ID + " integer primary key, "
                + NAME + " TEXT, "
                + TAG + " TEXT, "
                + STATUS + " varchar, "
                + IMGPATH + " TEXT, "
                + IMG + " BLOB )";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void insert(String name, String tag, String status, String imgPath, byte[] img) {
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        String sql = "insert into " + TABLE_NAME
                + " ("
                + ID + ","
                + NAME + ","
                + TAG + ","
                + STATUS + ","
                + IMGPATH + ","
                + IMG
                + ")"
                + " values ("
                + getCursor().getCount() + ",\'"
                + name + "\',\'"
                + tag + "\',\'"
                + status + "\',\'"
                + imgPath + "\',\'"
                + img
                + "\')";
        sqLiteDatabase.execSQL(sql);
    }

    public void updataStatus(String tag, String status) {
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        String sql = "updata " + TABLE_NAME + " set " + STATUS + " = \'" + status + "\' where " + TAG + " = \'" + tag + "\'";
        sqLiteDatabase.execSQL(sql);
    }

    public void delete(String tag) {
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        String sql = "delete from " + TABLE_NAME + " where " + TAG + " = \'" + tag + "\'";
        sqLiteDatabase.execSQL(sql);
    }

    public void delAll() {
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        String sql = "delete from " + TABLE_NAME;
        sqLiteDatabase.execSQL(sql);
    }

    public Cursor getCursor() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
        return cursor;
    }

    private List list = new ArrayList();
    public List<String> queryAllTag() {
        list.clear();
        Cursor cursor = getCursor();
        if (cursor != null && cursor.moveToFirst()) {
            if (cursor.getCount() > 0) {
                String ss = cursor.getString(cursor.getColumnIndex("Tag"));
                list.add(ss);
                while (cursor.moveToNext()) {
                    String s = cursor.getString(cursor.getColumnIndex("Tag"));
                    list.add(s);
                }
            }

        }
        return list;
    }

    private List<List> listALl = new ArrayList();
    public List<List> query() {
        listALl.clear();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME , null);
        if (cursor != null && cursor.moveToFirst()) {
            if (cursor.getCount() > 0) {
                add(cursor);
                while (cursor.moveToNext()) {
                    add(cursor);
                }
            }
        }
        cursor.close();
        return listALl;
    }

    private void add(Cursor cursor) {
        String id = cursor.getString(cursor.getColumnIndex("ID"));
        String name = cursor.getString(cursor.getColumnIndex("Name"));
        String tag = cursor.getString(cursor.getColumnIndex("Tag"));
        String status = cursor.getString(cursor.getColumnIndex("Status"));
        String imgPath = cursor.getString(cursor.getColumnIndex("ImgPath"));
        byte[] img = cursor.getBlob(cursor.getColumnIndex("Img"));
        List list = new ArrayList();
        list.add(id);
        list.add(name);
        list.add(tag);
        list.add(status);
        list.add(imgPath);
        list.add(img);
        listALl.add(list);
    }

    public byte[] img(Bitmap bitmap) {
        if (bitmap == null)
            return null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        try {
            bitmap.recycle();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    public byte[] readImage(String tag) {
        Cursor cursor = getReadableDatabase().rawQuery("select * from " + TABLE_NAME + " where Tag = \'" + tag + "\'",null);
        byte[] imgData = null;
        if (cursor.moveToFirst()) {
            //将Blob数据转化为字节数组
            imgData = cursor.getBlob(cursor.getColumnIndex("Img"));
        }
        return imgData;
    }
}
